package org.mspring.mlog.service.stat.impl;

import java.util.List;

import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

import org.mspring.mlog.entity.bbs.Topic;
import org.mspring.mlog.entity.stat.TopicStat;
import org.mspring.mlog.service.bbs.TopicService;
import org.mspring.mlog.service.stat.TopicStatService;
import org.mspring.platform.cache.service.CacheService;
import org.mspring.platform.dao.hibernate.GenericDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author Gao Youbo
 * @since 2013年9月5日
 */
@Service
@Transactional
public class TopicStatServiceImpl extends GenericDao<TopicStat, Long> implements TopicStatService {

    @Autowired
    private CacheService cacheService;
    @Autowired
    private TopicService topicService;

    // 缓存帖子点击率的缓存对象名称
    public static final String TOPIC_VIEW_CACHENAME = "TOPIC_VIEW";

    @Override
    public Long views(Long topic) {
        // TODO Auto-generated method stub
        if (topic == null) {
            return Long.valueOf(0);
        }
        TopicStat stat = get(topic);
        if (stat == null) {
            Topic t = topicService.get(topic);
            if (t == null) {
                return Long.valueOf(0);
            }
            stat = createTopicStat(topic, null, null);
        }
        Object value = cacheService.getCacheValue(TOPIC_VIEW_CACHENAME, topic.toString());
        Long views = null;
        if (value == null) {
            views = stat.getViews() + 1;
        } else {
            views = Long.valueOf(value.toString()) + 1;
        }
        cacheService.updateCacheValue(TOPIC_VIEW_CACHENAME, topic.toString(), views);
        return views;
    }

    @Override
    public void views2DB() {
        // TODO Auto-generated method stub
        Ehcache cache = cacheService.getCache(TOPIC_VIEW_CACHENAME);
        List<String> keys = cache.getKeys();
        if (keys == null || keys.size() <= 0) {
            return;
        }
        for (String id : keys) {
            Element e = cache.get(id);
            Long views = Long.parseLong(e.getValue().toString());
            updateTopicStat(Long.parseLong(id), views, null);
        }
        cache.removeAll();
    }

    @Override
    public TopicStat createTopicStat(Long topic, Long views, Long replys) {
        // TODO Auto-generated method stub
        if (topic == null) {
            return null;
        }
        if (views == null) {
            views = Long.valueOf(0);
        }
        if (replys == null) {
            replys = Long.valueOf(0);
        }
        TopicStat stat = new TopicStat(topic, views, replys);
        return super.save(stat);
    }

    @Override
    public void updateTopicStat(Long topic, Long views, Long replys) {
        // TODO Auto-generated method stub
        TopicStat stat = super.get(topic);
        if (stat == null) {
            return;
        }
        if (views != null) {
            stat.setViews(views);
        }
        if (replys != null) {
            stat.setReplys(replys);
        }
        super.update(stat);
    }
}
